import helper from '../../helper';
import {actions} from '../base';
import Vue from "vue";

let url = 'admin/api/manager';

export default {
    namespaced: true,
    state: {
        token: helper.getCache('token', ''),
        info: helper.getCache('info', {}),
        oneClickToken: helper.getCache('one_click_token', '')
    },
    mutations: {
        SET_TOKEN (state, token) {
            state.token = token;
            helper.setCache('token', token);
        },
        SET_INFO (state, info) {
            state.info = info;
            helper.setCache('info', info);
        },
        SET_ONE_CLICK_TOKEN (state, token) {
            state.token = token;
            helper.setCache('one_click_token', token);
        }
    },
    actions: Object.assign(actions(url), {

        Info ({commit, state}) {
            return new Promise((resolve, reject) => {
                Vue.axios.get(`${url}/info`).then(res => {
                    commit('SET_INFO', res.data.data);
                    return resolve(state.info);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
        Login ({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                Vue.axios.post(`admin/login`, userInfo).then(res => {
                    commit('SET_TOKEN', res.data.data);
                    return resolve(res);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
        SSOLogin({commit}, token) {
            return new Promise((resolve, reject) => {
                Vue.axios.post('admin/login/sso', {token: token}).then(res => {
                    commit('SET_TOKEN', res.data.data);
                    return resolve(res);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
        LoginQrcode ({commit}, params) {
            return Vue.axios.get('admin/login/qrcode', {
                params: params,
            });
        },
        LoginQrcodeQuery({commit}, params) {
            return Vue.axios.get(`admin/login/qrcode-query`, {
                params: params,
            });
        },
        Logout ({commit}) {
            return new Promise((resolve, reject) => {
                Vue.axios.get(`${url}/logout`).then(res => {
                    // 清除缓存
                    helper.clearCache();
                    return resolve(res);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
        Search({commit}, filter) {
            return Vue.axios.get(`${url}/search`, {
                params: filter
            });
        },
        BindQrcode ({commit}, params) {
            return Vue.axios.get(`${url}/bind-qrcode`, {
                params: params,
            });
        },
        BindQuery({commit}, params) {
            return Vue.axios.get(`${url}/bind-query`, {
                params: params,
            });
        },
        SendCode ({commit}, data) {
            return Vue.axios.post('admin/login/sms-code', data);
        },
        UpdateSimple ({commit}, data) {
            return Vue.axios.put(`${url}/simple`, data);
        },
        OneClickLogin ({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                Vue.axios.post(`${url}/one-click-login`, userInfo).then(res => {
                    commit('SET_ONE_CLICK_TOKEN', res.data.data);
                    return resolve(res);
                }).catch(error => {
                    return reject(error);
                });
            });
        },
    }),
    getters: {

    }
};
